package org.neowei.image.filtering;

import java.awt.image.BufferedImage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.neowei.image.filter.FilterOption;
import org.neowei.image.filter.HorizontalFilter;

public class HorizontalFiltering {
	final private int srcheight;
	final private int srcwidth;
	final private int neowidth;
	final private double[] normalContrib;
	final private int numberhalfdots;
	final private BufferedImage outimage;
	final private BufferedImage srcimage;

	public HorizontalFiltering(FilterOption contributor, int srcwidth, int srcheight, int neowidth, BufferedImage srcimage) {
		this.normalContrib = contributor.getNormalContrib();
		this.numberhalfdots = contributor.getNumberhalfdots();
		this.srcheight = srcheight;
		this.srcwidth = srcwidth;
		this.neowidth = neowidth;
		this.srcimage = srcimage;
		outimage = new BufferedImage(this.neowidth, this.srcheight, BufferedImage.TYPE_INT_RGB);
	}

	public BufferedImage getBufferedImage() {
		ExecutorService service = Executors.newCachedThreadPool();
		for (int x = 0; x <= this.neowidth; x++) {
			int start = 0;
			int X = (int) ((double) x * srcwidth / neowidth + 0.5);
			int startX = X - numberhalfdots;
			if (startX < 0) {
				startX = 0;
				start = numberhalfdots - X;
			}
			int stopX = X + numberhalfdots;
			if (stopX > (srcwidth - 1)) {
				stopX = srcwidth - 1;
			}
			service.submit(new HorizontalFilter(outimage, srcimage, startX, stopX, start, x, normalContrib, srcheight));
		}
		service.shutdown();
		while (!service.isTerminated())
			;
		return this.outimage;
	}
}
